﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using SP = Microsoft.SharePoint.Client;

namespace com.sharepointutils.application.Forms
{
    public partial class DownloadForm : Form
    {
        SharePointConnection Connection { get; set; }
        String List { get; set; }

        public DownloadForm(SharePointConnection connection)
        {
            InitializeComponent();
            this.Text = "Download - " + connection.List;
            this.Connection = connection;
            this.List = connection.List;
            this.Tag = connection.Label;

            Icon iconForFolder = com.sharepointutils.application.Properties.Resources.Folder;
            imageList.Images.Add("folder", iconForFolder);
        }


        private void btn_download_Click(object sender, EventArgs e)
        {
            try
            {
                if (folderBrowser.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    foreach (SP.ListItem x in SharePointUtilities.getAllListItems(Connection.URL, List, Connection.Credentials))
                    {
                        String path = Path.GetFileName(x["FileRef"].ToString());
                        using (Stream fs = File.Create(folderBrowser.SelectedPath + "\\" + path))
                        {
                            SharePointUtilities.CopyStream(SharePointUtilities.DownloadListItem(Connection.URL, x), fs);
                        }
                    }
                    String[] files = Directory.GetFiles(folderBrowser.SelectedPath);
                    loadFilesToList(files, ref listView_download);
                    String[] folders = Directory.GetDirectories(folderBrowser.SelectedPath);
                    loadFilesToList(folders, ref listView_download);
                }
            }
            catch (Exception ex)
            {
                //txt_errorLog.Text += Environment.NewLine + "Error downloading: " + ex.Message;
            }
        }

        private void loadFilesToList(String[] files, ref ListView lv)
        {
            foreach (var f in files)
            {
                if ((File.GetAttributes(f) & FileAttributes.Directory) != FileAttributes.Directory)
                {
                    Icon iconForFile = SystemIcons.WinLogo;
                    FileInfo file = new FileInfo(f);
                    ListViewItem item = new ListViewItem(file.Name, 1);
                    iconForFile = Icon.ExtractAssociatedIcon(file.FullName);

                    if (!imageList.Images.ContainsKey(file.Extension))
                    {
                        // If not, add the image to the image list.
                        iconForFile = System.Drawing.Icon.ExtractAssociatedIcon(file.FullName);
                        imageList.Images.Add(file.Extension, iconForFile);
                    }
                    item.ImageKey = file.Extension;
                    item.Tag = f;
                    lv.Items.Add(item);
                }
                else
                {
                    DirectoryInfo folder = new DirectoryInfo(f);
                    ListViewItem item = new ListViewItem(folder.Name);
                    item.ImageKey = "folder";
                    item.Tag = folder.FullName;
                    lv.Items.Add(item);
                }
            }
            lv.Update();
        }
    }
}
